import matplotlib.pyplot as plt
import numpy as np
from process import utils

y1 = [48.17, 52.31, 61.25, 72.36]
y2 = [49.03, 57.21, 75.13, 82.26]
y3 = [52.34, 61.59, 77.84, 92.07]
y4 = [50.86, 62.41, 79.33, 89.85]

aRR_y1 = [[44.39, 51.95, 57.15, 39.19, 56.18, 40.16, 48.99, 47.35, 49.18, 47.16, 51.15, 45.19, 50.81, 45.53, 44.66, 51.68, 44.97, 51.37, 55.8, 40.54, 48.88, 47.46, 48.08, 48.26, 49.52, 46.82, 52.18, 44.16, 52.46, 43.88], [50.68, 53.94, 53.58, 51.04, 55.26, 49.36, 48.44, 56.18, 52.37, 52.25, 56.75, 47.87, 53.6, 51.02, 56.28, 48.34, 51.27, 53.35, 57.01, 47.61, 48.33, 56.29, 48.24, 56.38, 59.83, 44.79, 51.26, 53.36, 47.11, 57.51], [64.54, 57.96, 60.29, 62.21, 70.25, 52.25, 53.01, 69.49, 66.66, 55.84, 56.01, 66.49, 58.75, 63.75, 53.4, 69.1, 53.79, 68.71, 66.06, 56.44, 63.91, 58.59, 65.04, 57.46, 69.8, 52.7, 59.13, 63.37, 60.51, 61.99], [73.56, 71.16, 73.31, 71.41, 68.37, 76.35, 75.02, 69.7, 69.36, 75.36, 72.9, 71.82, 70.08, 74.64, 74.44, 70.28, 72.53, 72.19, 72.13, 72.59, 75.75, 68.97, 72.69, 72.03, 70.83, 73.89, 76.19, 68.53, 69.83, 74.89]]
aRR_y2 = [[49.6, 48.46, 56.08, 41.98, 53.1, 44.96, 53.06, 45.0, 46.24, 51.82, 53.07, 44.99, 39.25, 58.81, 55.05, 43.01, 55.15, 42.91, 53.01, 45.05, 50.87, 47.19, 48.77, 49.29, 52.46, 45.6, 43.42, 54.64, 55.64, 42.42], [53.71, 60.71, 57.19, 57.23, 57.2, 57.22, 66.91, 47.51, 49.46, 64.96, 64.75, 49.67, 54.19, 60.23, 48.61, 65.81, 60.37, 54.05, 53.09, 61.33, 55.25, 59.17, 58.04, 56.38, 57.37, 57.05, 61.12, 53.3, 52.27, 62.15], [82.57, 67.69, 68.85, 81.41, 68.94, 81.32, 77.5, 72.76, 68.6, 81.66, 73.91, 76.35, 69.16, 81.1, 69.26, 81.0, 79.85, 70.41, 74.36, 75.9, 69.29, 80.97, 77.05, 73.21, 71.05, 79.21, 69.9, 80.36, 77.97, 72.29], [92.27, 72.25, 83.68, 80.84, 84.54, 79.98, 70.05, 94.47, 74.81, 89.71, 90.13, 74.39, 86.21, 78.31, 75.61, 88.91, 92.19, 72.33, 71.28, 93.24, 91.11, 73.41, 71.53, 92.99, 85.04, 79.48, 79.49, 85.03, 85.37, 79.15]]
aRR_y3 = [[39.98, 64.7, 56.28, 48.4, 39.39, 65.29, 55.66, 49.02, 66.03, 38.65, 54.83, 49.85, 54.67, 50.01, 49.54, 55.14, 66.98, 37.7, 66.12, 38.56, 38.25, 66.43, 66.52, 38.16, 44.78, 59.9, 59.36, 45.32, 49.85, 54.83], [71.27, 51.91, 61.55, 61.63, 55.51, 67.67, 73.14, 50.04, 58.6, 64.58, 59.25, 63.93, 64.78, 58.4, 64.19, 58.99, 67.71, 55.47, 53.0, 70.18, 62.69, 60.49, 73.21, 49.97, 60.78, 62.4, 62.64, 60.54, 59.25, 63.93], [65.32, 90.36, 70.92, 84.76, 78.37, 77.31, 74.09, 81.59, 85.67, 70.01, 74.64, 81.04, 85.02, 70.66, 81.17, 74.51, 84.16, 71.52, 71.77, 83.91, 64.0, 91.68, 88.61, 67.07, 79.74, 75.94, 75.23, 80.45, 76.61, 79.07], [94.08, 90.06, 97.14, 87.0, 87.03, 97.11, 85.66, 98.48, 90.19, 93.95, 97.35, 86.79, 85.61, 98.53, 93.51, 90.63, 97.31, 86.83, 91.05, 93.09, 86.79, 97.35, 84.44, 99.7, 90.85, 93.29, 93.02, 91.12, 99.95, 84.19]]
aRR_y4 = [[45.66, 56.06, 58.97, 42.75, 63.58, 38.14, 47.94, 53.78, 39.12, 62.6, 59.95, 41.77, 46.81, 54.91, 62.48, 39.24, 48.37, 53.35, 65.22, 36.5, 54.98, 46.74, 56.45, 45.27, 57.89, 43.83, 41.63, 60.09, 52.12, 49.6], [48.27, 76.55, 65.15, 59.67, 43.9, 80.92, 79.53, 45.29, 68.44, 56.38, 76.75, 48.07, 61.05, 63.77, 56.64, 68.18, 67.8, 57.02, 48.57, 76.25, 62.1, 62.72, 79.51, 45.31, 68.65, 56.17, 69.29, 55.53, 63.66, 61.16], [74.18, 84.48, 67.44, 91.22, 82.3, 76.36, 76.07, 82.59, 75.35, 83.31, 72.12, 86.54, 89.45, 69.21, 83.32, 75.34, 75.09, 83.57, 80.95, 77.71, 73.39, 85.27, 84.77, 73.89, 76.04, 82.62, 84.19, 74.47, 79.4, 79.26], [85.78, 93.92, 81.35, 98.35, 85.19, 94.51, 93.55, 86.15, 96.52, 83.18, 99.05, 80.65, 81.26, 98.44, 80.49, 99.21, 86.06, 93.64, 92.61, 87.09, 89.1, 90.6, 98.11, 81.59, 99.23, 80.47, 92.91, 86.79, 91.4, 88.3]]

np.random.seed(1)
ar = np.array(y1)
aRR = [] # 存放结果
for i in range(ar.shape[0]):
    CONST = np.random.randint(5, 12)  # 浮动大小，就是[平均值-CONST, 平均值+CONST]之间的随机数
    tmp = []
    num = 30
    for j in range(num // 2):
        tmp.extend(utils.float_random(ar[i], ar[i] - CONST, ar[i] + CONST)) # 每次生成2个随机数
    aRR.append(tmp)
    # print(np.mean(np.array(tmp)))

np.savetxt("./csv/figure12a.csv", aRR, delimiter=",", fmt="%.2f")
print(aRR)

x = [2,3,4,5]

aRR_std1 = np.std(aRR_y1,axis=1)
aRR_std2 = np.std(aRR_y2,axis=1)
aRR_std3 = np.std(aRR_y3,axis=1)
aRR_std4 = np.std(aRR_y4,axis=1)

arr = np.array([y1, y2, y3, y4])
mean = np.mean(arr, axis=0)
std = np.std(arr, axis=0)

plt.plot(x, y1, 'bo-', markersize=8, label='LDC-COR')
plt.plot(x, y2, 'gv--', color='#00FF00', markersize=8, label='LDC-OR')
plt.plot(x, y3, 'r^--', markersize=8, label='LDC-ACOR')
plt.plot(x, y4, 'ks--', color='black', markersize=8, label='LDC-AOR')

plt.errorbar(x, y1, yerr=aRR_std1, fmt="bo-", color="blue", elinewidth=2, capsize=6,markersize=8)
plt.errorbar(x, y2, yerr=aRR_std2, fmt="v--", color='#00FF00', elinewidth=2, capsize=6,markersize=8)
plt.errorbar(x, y3, yerr=aRR_std3, fmt='r^--', elinewidth=2, capsize=4, markersize=8)
plt.errorbar(x, y4, yerr=aRR_std4, fmt='ks--', elinewidth=2, capsize=4, markersize=8)

plt.xticks([2,2.5,3,3.5,4,4.5,5], fontsize='14')
plt.yticks([40,50,60,70,80,90,100], fontsize='14')
plt.grid(linewidth=0.4, color='#DFDFDF')
plt.xlabel('K', fontsize=18)
plt.ylabel('Number of Transmissions', fontsize=18)
# plt.legend(['LDC-COR', 'LDC-OR', 'LDC-ACOR', 'LDC-AOR', 'AVERAGE'], loc='upper left', fontsize=14)
plt.legend(['LDC-COR', 'LDC-OR', 'LDC-ACOR', 'LDC-AOR'], loc='upper left', fontsize=14)

plt.tight_layout()
plt.savefig('./eps/figure12a.eps', dpi=600, format='eps')
plt.show()